package cn.cvs.controller;

import cn.cvs.pojo.Admin;
import cn.cvs.pojo.GiveApply;
import cn.cvs.pojo.Pet;
import cn.cvs.pojo.Review;
import cn.cvs.service.giveapply.GiveApplyService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sys/give")
public class GiveApplyController {

    @Resource
    private GiveApplyService giveApplyService;

    @RequestMapping(value="/toList")
    public String queryPageList(Model model,
                                @RequestParam(value = "state",required = false) Integer state,
                                @RequestParam(value ="pageIndex", defaultValue = "1") Integer pageIndex ) {
        //设置页面容量
        Integer pageSize = Constants.pageSize;

        int totalCount = giveApplyService.count(state);

        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(pageIndex);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();

        List<GiveApply> giveApplies = giveApplyService.list(state, pageIndex, pageSize);

        if(pageIndex > totalPageCount){
            pageIndex = totalPageCount;
        }

        model.addAttribute("giveApplies", giveApplies);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", pageIndex);

        return "give/list";
    }

    /**
     *
     * @param giveApply
     * @return
     */
    @PostMapping("/modify")
    public String modify(GiveApply giveApply){
        int result = giveApplyService.updateById(giveApply);
        if(result <= 0){
            return "give/update";
        }
        return "redirect:/sys/give/toList";
    }

    /**
     * 查询回访详情信息
     * @param id
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable Integer id, Model model, HttpServletRequest request){
        GiveApply giveApply = giveApplyService.selectById(id);
        model.addAttribute("giveApply",giveApply);
        return "give/update";
    }
}
